package com.awz.tiger.socket;

import java.io.*;
import java.net.*;

public class DailyAdviceServer {

	 String[] adviceList = {"Take smaller bites", "So far, So good", "Nerver lose hope", "Nerver say Never"};
	 public void go()
	 {
		 try
		 {
			 ServerSocket serverSock = new ServerSocket(4242);
			 
			 while(true)
			 {
				 Socket sock = serverSock.accept();
				 PrintWriter writer = new PrintWriter(sock.getOutputStream());
				 String advice = getAdvice();
				 writer.println(advice);
				 writer.close();
				 System.out.println("Server said: " + advice );
			 }
		 }
		 catch(Exception ex)
		 {
			 ex.printStackTrace();
		 }
	 }
	 
	 public static void main(String[] args)
	 {
		 DailyAdviceServer server = new DailyAdviceServer();
		 server.go();
	 }
	 
	 private String getAdvice()
	 {
		 int random = (int)(Math.random() * adviceList.length);
		 return adviceList[random];
	 }
}
